<template>
  <el-row>
    <el-card>
      <el-form ref="searchForm" :inline="true" :model="searchForm">
        <el-form-item :label="$t('reportModule.loanDate.date')">
          <el-date-picker
            v-model="time"
            type="daterange"
            range-separator="~"
            :start-placeholder="$t('common.startDate')"
            :end-placeholder="$t('common.endDate')"
          />
        </el-form-item>
        <el-form-item :label="$t('reportModule.vtg.reBorrow')">
          <el-select v-model="searchForm.firstLoan" clearable :placeholder="$t('common.select')">
            <el-option
              v-for="item in $t('reportModule.vtg.borrowData')"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('reportModule.loanDate.productType')">
          <el-select v-model="searchForm.loanProductType" clearable :placeholder="$t('common.select')">
            <el-option
              v-for="item in $t('reportModule.loanDate.productTypes')"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('reportModule.vtg.money')">
          <el-row>
            <el-col :span="11">
              <el-input
                v-model="searchForm.applyMoneyMin"
                :maxlength="20"
                :placeholder="$t('common.input')"
                @input="test('applyMoneyMin')"
              />
            </el-col>
            <el-col :span="2" style="text-align: center">~</el-col>
            <el-col :span="11">
              <el-input
                v-model="searchForm.applyMoneyMax"
                :maxlength="20"
                :placeholder="$t('common.input')"
                @input="test('applyMoneyMax')"
              />
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item :label="$t('reportModule.vtg.period')">
          <el-row>
            <el-col :span="11">
              <el-input
                v-model="searchForm.loanDaysMin"
                :maxlength="20"
                :placeholder="$t('common.input')"
                @input="test('loanDaysMin')"
              />
            </el-col>
            <el-col :span="2" style="text-align: center">~</el-col>
            <el-col :span="11">
              <el-input
                v-model="searchForm.loanDaysMax"
                :maxlength="20"
                :placeholder="$t('common.input')"
                @input="test('loanDaysMax')"
              />
            </el-col>
          </el-row>

        </el-form-item>
        <el-form-item :label="$t('reportModule.vtg.channelSource')">
          <el-select v-model="searchForm.channel" clearable :placeholder="$t('common.select')" @change="setSource">
            <el-option
              v-for="item in $t('reportModule.vtg.source')"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('reportModule.vtg.promotionMethod')">
          <el-select v-model="searchForm.promotionTypeStr" clearable multiple collapse-tags :placeholder="$t('common.input')">
            <el-option
              v-for="item in promotionData"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('reportModule.vtg.referrer')">
          <el-input v-model="searchForm.referrerName" clearable :placeholder="$t('common.input')" :maxlength="50" />
        </el-form-item>
        <el-row>
          <el-button type="primary" @click="search">
            {{ $t('common.search') }}
          </el-button>
          <el-button @click="reset">
            {{ $t('common.reset') }}
          </el-button>
          <el-button :disabled="!(totalNumber > 0)" @click="exportList">
            {{ $t('common.ex') }}
          </el-button>
        </el-row>
      </el-form>
    </el-card>
    <el-card>
      <el-row>
        <h3>{{ $t('reportModule.conversionData.total') }}</h3>
        <el-divider />
        <vtg-loan-number
          :table-data="tableDataTotal"
          :lang="totalLoading"
          :date="false"
        />
      </el-row>
    </el-card>
    <el-card>
      <el-row>
        <h3>{{ $t('reportModule.conversionData.detail') }}</h3>
        <el-divider />
        <vtg-loan-number
          :table-data="tableData"
          :lang="loading"
          :date="true"
        />
        <div class="block">
          <el-pagination
            :current-page="searchForm.currentPage"
            :page-sizes="[10, 20, 30, 40, 50]"
            :page-size="searchForm.pageSize"
            :total="totalNumber"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-row>
    </el-card>
  </el-row>
</template>

<script>
import { paymentCount, paymentCountTotal, paymentCountExport } from '@/api/report'
import { formatDate } from '@/utils/method'
import vtgLoanNumber from '@/components/report/vtg-loan-number'
export default {
  name: 'Number',
  components: {
    vtgLoanNumber
  },
  props: {
    allPackageData: {
      type: Array,
      default: function() {
        return []
      }
    }
  },
  data() {
    return {
      loading: false,
      totalLoading: false,
      tableData: [],
      tableDataTotal: [],
      tableText: {},
      totalNumber: 0,
      promotionData: [],
      time: [new Date((new Date()).getTime() - 1000 * 60 * 60 * 24 * 6), new Date()],
      searchForm: this.params()
    }
  },
  mounted() {
    this.tableText = this.$t('reportModule.loanDate.amount')
    this.getData()
    this.getTotalData()
  },
  methods: {
    params() {
      return {
        currentPage: 1,
        pageSize: 10,
        paymentTimeStart: null,
        paymentTimeEnd: null,
        firstLoan: '',
        applyMoneyMin: null,
        applyMoneyMax: null,
        loanDaysMin: null,
        loanDaysMax: null,
        channel: '',
        promotionTypeStr: [],
        referrerName: null,
        deviceType: '',
        loanProductType: ''
      }
    },
    search() {
      this.searchForm.currentPage = 1
      this.getData()
      this.getTotalData()
    },
    reset() {
      this.searchForm = this.params()
      this.time = [new Date((new Date()).getTime() - 1000 * 60 * 60 * 24 * 6), new Date()]
      this.getData()
      this.getTotalData()
    },
    test(data) {
      let value = this.searchForm[data]
      if (value.length > 0) {
        value = value.replace(/[^0-9]/g, '')
      }
      this.searchForm[data] = value
    },
    searchData() {
      const data = this.searchForm
      if (this.time && this.time.length > 1) {
        data['paymentTimeStart'] = formatDate(new Date(this.time[0]), 'yyyy-MM-dd') + ' 00:00:00'
        data['paymentTimeEnd'] = formatDate(new Date(this.time[1]), 'yyyy-MM-dd') + ' 23:59:59'
      } else {
        data['paymentTimeStart'] = null
        data['paymentTimeEnd'] = null
      }
      if (this.searchForm.loanDaysMin && this.searchForm.loanDaysMax) {
        data['loanDaysMin'] = this.searchForm.loanDaysMin
        data['loanDaysMax'] = this.searchForm.loanDaysMax
      }
      if (this.searchForm.applyMoneyMin && this.searchForm.applyMoneyMax) {
        data['applyMoneyMin'] = this.searchForm.applyMoneyMin
        data['applyMoneyMax'] = this.searchForm.applyMoneyMax
      }
      return data
    },
    getData() {
      this.loading = true
      paymentCount(this.searchData())
        .then(res => {
          this.loading = false
          if (res.success) {
            this.tableData = res.data
            this.totalNumber = res.page ? res.page['totalNumber'] : 0
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    getTotalData() {
      this.totalLoading = true
      paymentCountTotal(this.searchData())
        .then(res => {
          this.totalLoading = false
          if (res.success) {
            this.tableDataTotal = [res.data]
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.totalLoading = false
          this.$message.error(err.message)
        })
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.searchForm.currentPage = val
      this.getData()
    },
    exportList() {
      paymentCountExport(this.searchData())
    },
    setSource() {
      const channel = this.searchForm.channel
      if (channel === 1) {
        this.promotionData = this.$t('usrModule.list.method')
      } else if (channel === 2) {
        this.promotionData = this.$t('usrModule.list.method1')
      } else if (channel === 3) {
        this.promotionData = []
        const arr = this.allPackageData.filter(v => {
          return v['mediaSource'] !== null
        })
        const node = []
        arr.forEach(v => {
          node.push({
            desc: v['mediaSource'],
            value: v['mediaSource']
          })
        })
        this.promotionData = node
      } else {
        this.promotionData = []
      }
    }
  }
}
</script>

<style lang="scss" scoped>
  .el-card{
    margin: 10px;
  }
</style>
